package tony.test.core.jackson

import com.fasterxml.jackson.databind.JsonNode
import org.junit.jupiter.api.Test
import tony.core.annotation.jackson.JacksonSerializeInject
import tony.core.jackson.InjectBeanSerializerModifier
import tony.core.jackson.SerializeInjector
import tony.core.jackson.SerializeInjectorFactory
import tony.core.jackson.SerializeInjectorSupplier
import tony.core.utils.createObjectMapper
import tony.core.utils.println

class OrgNameSerializeInjectorSupplier(override val name: String = "orgName") : SerializeInjectorSupplier<OrgNameSerializeInjector> {
    override fun get(): OrgNameSerializeInjector =
        OrgNameSerializeInjector()
}


class OrgNameSerializeInjector : SerializeInjector {
    override var params: MutableList<String> = ArrayList()
    override var targetFieldName: String = ""
    override fun inject(source: JsonNode): Any {
        return source.get(params.getOrNull(0).orEmpty())?.asText() + "hello"
    }
}

class JacksonSerializeInjectTest {

    @Test
    fun descriptorTest() {
        val objectMapper = createObjectMapper().apply {
            val serializeInjectorFactory = SerializeInjectorFactory()
            val orgNameSerializeInjectorSupplier = OrgNameSerializeInjectorSupplier()
            serializeInjectorFactory.register(orgNameSerializeInjectorSupplier.name, orgNameSerializeInjectorSupplier)
            serializeInjectorFactory.register("orgName1", orgNameSerializeInjectorSupplier)
            serializerFactory =
                serializerFactory.withSerializerModifier(InjectBeanSerializerModifier(serializeInjectorFactory))
        }
        objectMapper.writeValueAsString(Org()).println()
    }
}


@JacksonSerializeInject(
    params = ["orgId"],
    targetFieldName = "orgName",
    injectorName = "orgName",
)
@JacksonSerializeInject(
    params = ["orgId"],
    targetFieldName = "orgName1",
    injectorName = "orgName1",
)
class Org {
    var orgId: String = " qq"
}
